﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 索引器
{
    class Box
    {
        private string[] names = { "length", "width", "height" };
        private double[] dimension = new double[3];

        public Box(double length, double width, double height)
        {
            dimension[0] = length;
            dimension[1] = width;
            dimension[2] = height;
        }//end constructor Box

        public double this[int index]
        {
            set
            {
                if (index < dimension.Length && index >= 0)
                {
                    dimension[index] = value;
                }
            }//end set
            get
            {
                if (index >= dimension.Length || index < 0)
                {
                    return -1;
                }
                else
                {
                    return dimension[index];
                }
            }//end get 
        }//end numeric indexer

        public double this[string name]
        {
            set
            {
                int i = 0;

                while (i < names.Length && names[i] != name.ToLower())
                {
                    i++;
                }

                if (i != names.Length)
                {
                    dimension[i] = value;
                }
            }//end set
            get
            {
                int i = 0;

                while (i < names.Length && name.ToLower() != names[i])
                {
                    i++;
                }

                return (names.Length == i) ? -1 : dimension[i];
                //if (i != names.Length)
                //{
                //    return dimension[i];
                //}
                //else
                //{
                //    return -1;
                //}
            }//end get
        }//end string indexer

    }
}
